package com.jjprojects.earutility;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
 * @author U678864
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class EARFilesComparingTool implements ActionListener
{
    private ImageIcon imgicnExcel = new ImageIcon("images/Excel.gif");
    private ImageIcon imgicnInternerExplore = new ImageIcon("images/InternerExplore.gif");
    private ImageIcon imgicnNotepad = new ImageIcon("images/Notepad.gif");
       
    JDialog dlgEARFileCompareTool = null;
    JTable tableEARCompare = null;
    
    Vector vecFirstEARFilesContent = null;
    Vector vecSecondEARFilesContent = null;

    Vector vecFirstEARFilesLastModifiedDate = null;
    Vector vecSecondEARFilesLastModifiedDate = null;

    Vector vecFirstEARFilesSize = null;
    Vector vecSecondEARFilesSize = null;
    
    Vector vecEqualityStatus = null;
    Vector vecEqualityStatusDetails = null;
    
    String strGlobleFirstEARFileName = null;
    String strGlobleSecondEARFileName = null;

    private JButton btnGenerateReport;
    JCheckBox chkMissingFiles;
    JCheckBox chkChangedFiles;
    JCheckBox chkSimilarFiles;
    JDialog dlgReportSelection;
    int intSimilarFileCount;
    int intModifiedFileCount;
    int intMissingFirstFileCount;
	int intMissingSecondFileCount;
	
    private String strReportType;
    private JButton btnHtmlReport;
    private JButton btnTextReport;
    
    String strFirstParentFileName = null;
    String strSecondParentFileName = null;
    
    
    public void compareFilesinEar(String strFirstParentFileName, String strSecondParentFileName, 
            String strFirstEARFileName, String strSecondEARFileName)
    {
        strGlobleFirstEARFileName = strFirstEARFileName;
        strGlobleSecondEARFileName = strSecondEARFileName;
        
        this.strFirstParentFileName = strFirstParentFileName;
        this.strSecondParentFileName = strSecondParentFileName;
        
        vecFirstEARFilesContent = new Vector(10000);
        vecSecondEARFilesContent = new Vector(10000);

        vecFirstEARFilesLastModifiedDate = new Vector(10000);
        vecSecondEARFilesLastModifiedDate = new Vector(10000);

        vecFirstEARFilesSize = new Vector(10000);
        vecSecondEARFilesSize = new Vector(10000);
        
        vecEqualityStatus = new Vector();
        vecEqualityStatusDetails = new Vector();

        try
        {
            ZipFile zipfilFirstEAR = new ZipFile(strFirstEARFileName);

            for (Enumeration enmEntries = zipfilFirstEAR.entries(); enmEntries.hasMoreElements();)
            {
                ZipEntry zipEtyEARFiles = ((ZipEntry) enmEntries.nextElement());
                String strEARFileDetails = zipEtyEARFiles.getName();
                if(strEARFileDetails.contains("."))
                {
                    vecFirstEARFilesContent.add(strEARFileDetails);
                    vecFirstEARFilesLastModifiedDate.add(zipEtyEARFiles.getTime()+"");
                    vecFirstEARFilesSize.add(zipEtyEARFiles.getSize()+"");
                }
            }
        }
        catch (IOException e)
        {
            JOptionPane.showMessageDialog(dlgEARFileCompareTool, "Error in opening the file"
                    , "File selection error", JOptionPane.ERROR_MESSAGE);
            EARUtility.log4jEARUtility.fatal("Exception", e);
            return;
        }
        //Storing the second ear file details
        try
        {
            ZipFile zipfilSecondEAR = new ZipFile(strSecondEARFileName);

            for (Enumeration enmEntries = zipfilSecondEAR.entries(); enmEntries.hasMoreElements();)
            {
                ZipEntry zipEtyEARFiles = ((ZipEntry) enmEntries.nextElement());
                String strEARFileDetails = zipEtyEARFiles.toString();
                if(strEARFileDetails.contains("."))
                {
                    vecSecondEARFilesContent.add(strEARFileDetails);
                    vecSecondEARFilesLastModifiedDate.add(zipEtyEARFiles.getTime()+"");
                    vecSecondEARFilesSize.add(zipEtyEARFiles.getSize()+"");
                }
            }
        }
        catch (IOException e)
        {
            EARUtility.log4jEARUtility.fatal("Exception", e);
        }
        int intReturnValue = arrangeVectorsInCorrectOrder();
        if(intReturnValue == 0)
        {
            setEARFilesComparisonDetails();
            showEARFilesCompareDialog();
        }
    }

    public int arrangeVectorsInCorrectOrder()
    {
        int intFirstEarSize = vecFirstEARFilesContent.size();
        int intSecondEarSize = vecSecondEARFilesContent.size();
        
        Vector vecMissedFilesBasicDetails = new Vector(1000);

        if(intFirstEarSize < intSecondEarSize)
        {
            //Take the Second ear file as Reference
            for(int i=0;i<intSecondEarSize;i++)
            {
                String strSecondEarContent = (String) vecSecondEARFilesContent.get(i);

                if(vecFirstEARFilesContent.contains(strSecondEarContent))
                {
                    int intPositionOfValueInFirstJar = vecFirstEARFilesContent.indexOf(strSecondEarContent);
                    if(intPositionOfValueInFirstJar >= 0)
                    {
                        if(intPositionOfValueInFirstJar > i)
                        {
                            int intRowsNeedToInsert = intPositionOfValueInFirstJar - i;
                            for(int j=0;j<intRowsNeedToInsert;j++)
                            {
                                vecSecondEARFilesContent.insertElementAt("", i);
                                vecSecondEARFilesLastModifiedDate.insertElementAt("", i);
                                vecSecondEARFilesSize.insertElementAt("", i);
                                i++;
                                intSecondEarSize++;
                            }
                        }
                    }
                }
                else
                {
                    vecMissedFilesBasicDetails.add(strSecondEarContent + " is missing in first file");
                    vecFirstEARFilesContent.insertElementAt("", i);
                    vecFirstEARFilesLastModifiedDate.insertElementAt("", i);
                    vecFirstEARFilesSize.insertElementAt("", i);
                }
            }
            
            intFirstEarSize = vecFirstEARFilesContent.size();
            intSecondEarSize = vecSecondEARFilesContent.size();
            
            int intSizeDifference = intFirstEarSize - intSecondEarSize;
            if(intSizeDifference > 0)
            {
                for(int i=0;i<intSizeDifference;i++)
                {
                    String strFirstFileContent = vecFirstEARFilesContent.get((intSecondEarSize + i)).toString();
                    strFirstFileContent = strFirstFileContent.replaceAll("/", ".");
                    vecMissedFilesBasicDetails.add(strFirstFileContent + " is missing in second file");
                    vecSecondEARFilesContent.add("");
                    vecSecondEARFilesLastModifiedDate.add("");
                    vecSecondEARFilesSize.add("");
                }
            }
        }
        else if(intFirstEarSize > intSecondEarSize)
        {
          //Take the First ear file as Reference
          for(int i=0;i<intFirstEarSize;i++)
          {
              String strFirstEarContent = (String) vecFirstEARFilesContent.get(i);
              
              if(vecSecondEARFilesContent.contains(strFirstEarContent))
              {
                  int intPositionOfValueInSecondJar = vecSecondEARFilesContent.indexOf(strFirstEarContent);
                  if(intPositionOfValueInSecondJar >= 0)
                  {
                      if(intPositionOfValueInSecondJar > i)
                      {
                          int intRowsNeedToInsert = intPositionOfValueInSecondJar - i;
                          for(int j=0;j<intRowsNeedToInsert;j++)
                          {
                              vecFirstEARFilesContent.insertElementAt("", i);
                              vecFirstEARFilesLastModifiedDate.insertElementAt("", i);
                              vecFirstEARFilesSize.insertElementAt("", i);
                              i++;
                              intFirstEarSize++;
                          }
                      }
                  }
              }
              else
              {
                  vecMissedFilesBasicDetails.add(strFirstEarContent + " is missing in second file");
                  vecSecondEARFilesContent.insertElementAt("", i);
                  vecSecondEARFilesLastModifiedDate.insertElementAt("", i);
                  vecSecondEARFilesSize.insertElementAt("", i);
              }
          }
          
          intFirstEarSize = vecFirstEARFilesContent.size();
          intSecondEarSize = vecSecondEARFilesContent.size();
          
          int intSizeDifference = intSecondEarSize - intFirstEarSize;
          if(intSizeDifference > 0)
          {
              for(int i=0;i<intSizeDifference;i++)
              {
                  String strSecondFileContent = vecSecondEARFilesContent.get((intFirstEarSize + i)).toString();
                  strSecondFileContent = strSecondFileContent.replaceAll("/", ".");
                  vecMissedFilesBasicDetails.add(strSecondFileContent + " is missing in first file");
                  
                  vecFirstEARFilesContent.add("");
                  vecFirstEARFilesLastModifiedDate.add("");
                  vecFirstEARFilesSize.add("");
              }
          }
        }
        else if(intFirstEarSize == intSecondEarSize)
        {
            String strFirstEarContent = null;
            String strSecondEarContent = null;
            
            for(int i=0;i<intFirstEarSize;i++)
            {
                strFirstEarContent = (String) vecFirstEARFilesContent.get(i);
                strSecondEarContent = (String) vecSecondEARFilesContent.get(i);
                if(!(vecSecondEARFilesContent.contains(strFirstEarContent)))
                {
                    vecFirstEARFilesContent.insertElementAt("", i);
                    vecFirstEARFilesLastModifiedDate.insertElementAt("", i);
                    vecFirstEARFilesSize.insertElementAt("", i);
                }
            }
            intFirstEarSize = vecFirstEARFilesContent.size();
            int intSizeDifference = intFirstEarSize - intSecondEarSize;
            int intFinalTotalSize = intFirstEarSize + intSizeDifference;
            if(intSizeDifference > 0)
            {
                for(int i=intFirstEarSize;i<intFinalTotalSize;i++)
                {
                    vecSecondEARFilesContent.add("");
                    vecSecondEARFilesLastModifiedDate.add("");
                    vecSecondEARFilesSize.add("");
                }
            }
            for(int i=0;i<intFirstEarSize;i++)
            {
                strFirstEarContent = (String) vecFirstEARFilesContent.get(i);
                strSecondEarContent = (String) vecSecondEARFilesContent.get(i);
                
                if(strFirstEarContent.equals(""))
                {
                    vecMissedFilesBasicDetails.add(strSecondEarContent + " is missing in first file");
                }
                if(strSecondEarContent.equals(""))
                {
                    vecMissedFilesBasicDetails.add(strFirstEarContent + " is missing in second file");
                }
            }
        }
        int intReturnValue = showErrorMessageDialog(vecMissedFilesBasicDetails);
        return intReturnValue;        
    }
    
    public int showErrorMessageDialog(Vector vecMissedFilesBasicDetails)
    {
        System.out.println("vecMissedFilesBasicDetails->" + vecMissedFilesBasicDetails);
        int intSize = vecMissedFilesBasicDetails.size();
        if(intSize >  20)
        {
            StringBuffer strbufHTMLContent = new StringBuffer("<HTML><BODY><B>More than 20 files are missing in the files.</B><BR>");
            strbufHTMLContent.append("Do you want to see the full details?");
            strbufHTMLContent.append("</BODY></HTML>");
            int intDialogSelection = JOptionPane.showConfirmDialog(dlgEARFileCompareTool, strbufHTMLContent.toString()
                    , "Missing file details", JOptionPane.YES_NO_OPTION);
             return intDialogSelection;
        }
        else if(intSize <= 0)
		{
			StringBuffer strbufHTMLContent = new StringBuffer("<HTML><BODY><B>The file names and count are same in both files</B><BR>");
            strbufHTMLContent.append("Do you want to see the full details?");
            strbufHTMLContent.append("</BODY></HTML>");
            int intDialogSelection = JOptionPane.showConfirmDialog(dlgEARFileCompareTool, strbufHTMLContent
                    , "Missing file details", JOptionPane.YES_NO_OPTION);
            return intDialogSelection;
		}
        else
        {
            StringBuffer strbufHTMLContent = new StringBuffer("<HTML><BODY><B>Missing File Details.</B><BR>");
            strbufHTMLContent.append("<UL>");
            for(int i=0;i<intSize;i++)
            {
                strbufHTMLContent.append("<LI>" + vecMissedFilesBasicDetails.get(i) + "</LI>");
            }
            strbufHTMLContent.append("</UL>");
            strbufHTMLContent.append("Do you want to see the full details?");
            strbufHTMLContent.append("</BODY></HTML>");
            int intDialogSelection = JOptionPane.showConfirmDialog(dlgEARFileCompareTool, strbufHTMLContent
                    , "Missing file details", JOptionPane.YES_NO_OPTION);
            return intDialogSelection;
        }
    }
    
    public void setEARFilesComparisonDetails()
    {
        int intSize = vecFirstEARFilesContent.size();
        String strFirstEarContent = null;
        String strSecondEarContent = null;
        for(int i=0;i<intSize;i++)
        {
            strFirstEarContent = (String) vecFirstEARFilesContent.get(i);
            strSecondEarContent = (String) vecSecondEARFilesContent.get(i);
            if(strFirstEarContent != null && strFirstEarContent.trim().length() != 0
                    && strSecondEarContent != null && strSecondEarContent.trim().length() != 0)
            {
                String strFirstEARFilesLastModifiedDate = (String) vecFirstEARFilesLastModifiedDate.get(i);
                String strSecondEARFilesLastModifiedDate = (String) vecSecondEARFilesLastModifiedDate.get(i);
                
                String strFirstEARFilesSize = (String) vecFirstEARFilesSize.get(i);
                String strSecondEARFilesSize = (String) vecSecondEARFilesSize.get(i);
                
                if(strFirstEARFilesLastModifiedDate.equals(strSecondEARFilesLastModifiedDate) && 
                        strFirstEARFilesSize.equals(strSecondEARFilesSize))
                {
                    vecEqualityStatus.add("EQUAL");
                    vecEqualityStatusDetails.add("Files have same size and last modified date");
                }
                else
                {
                    if(!(strFirstEARFilesSize.equals(strSecondEARFilesSize)))
                    {
                        vecEqualityStatus.add("NOT EQUAL");
                        vecEqualityStatusDetails.add("Files have different file size");
                    }
                    else if(!(strFirstEARFilesLastModifiedDate.equals(strSecondEARFilesLastModifiedDate)))
                    {
                        vecEqualityStatus.add("NOT EQUAL");
                        vecEqualityStatusDetails.add("Files have different last modified date");
                    }
                }
            }
            else
            {
                if(strFirstEarContent != null && strFirstEarContent.trim().length() == 0)
                {
                    vecEqualityStatus.add("FILE MISSING");
                    vecEqualityStatusDetails.add("File " + strSecondEarContent + " is missing in First JAR file");
                }
                else if (strSecondEarContent != null && strSecondEarContent.trim().length() == 0)
                {
                    vecEqualityStatus.add("FILE MISSING");
                    vecEqualityStatusDetails.add("File " + strFirstEarContent + " is missing in Second JAR file");
                }
            }
        }
    }

    public void showEARFilesCompareDialog()
    {
        int intTotalNumberOfRecords = vecSecondEARFilesContent.size();
        dlgEARFileCompareTool = new JDialog(EARUtility.frmEARUtility, "EAR Compare details", true);

        JPanel panlFirstEARFile = new JPanel();
        JPanel panlSecondEARFile = new JPanel();

        CustomAbstractTableModel cstbAddFields = new CustomAbstractTableModel(
                this, intTotalNumberOfRecords, 3, true, 1);//rows,cols
        
        int intWidthForEachColumn = (EARUtility.intActualWidth - 16) / 2;
        
        TableColumn tabcolFirstEarContent = null;
        TableColumn tabcolEqualOrNot = null;
        TableColumn tabcolSecondEarContent = null;
        
        if(this.strFirstParentFileName.equals("") || this.strSecondParentFileName.equals(""))
        {
            tabcolFirstEarContent = TableDesigner.createColumn(0, intWidthForEachColumn, strGlobleFirstEARFileName);
            tabcolEqualOrNot = TableDesigner.createColumn(1, 16, "");
            tabcolSecondEarContent = TableDesigner.createColumn(2, intWidthForEachColumn, strGlobleSecondEARFileName);
        }
        else
        {
            int intFirstFileLastIndex = strGlobleFirstEARFileName.lastIndexOf("\\");
            int intSecondFileLastIndex = strGlobleSecondEARFileName.lastIndexOf("\\");
            
            String strOnlyCurrentFirstFileName = strGlobleFirstEARFileName.substring(intFirstFileLastIndex);
            String strOnlyCurrentSecondFileName = strGlobleSecondEARFileName.substring(intSecondFileLastIndex);
            
            strFirstParentFileName = strFirstParentFileName + strOnlyCurrentFirstFileName;
            strSecondParentFileName = strSecondParentFileName + strOnlyCurrentSecondFileName;
            
            
            tabcolFirstEarContent = TableDesigner.createColumn(0, intWidthForEachColumn, strFirstParentFileName);
            tabcolEqualOrNot = TableDesigner.createColumn(1, 16, "");
            tabcolSecondEarContent = TableDesigner.createColumn(2, intWidthForEachColumn, strSecondParentFileName);
        }
        
        tabcolFirstEarContent.setMinWidth(intWidthForEachColumn);
        tabcolEqualOrNot.setMinWidth(16);
        tabcolEqualOrNot.setMaxWidth(16);
        tabcolSecondEarContent.setMinWidth(intWidthForEachColumn);

        TableColumnModel tabcolmdlRecycleBinManager = TableDesigner
                .createColumnModelForThreeRow(tabcolFirstEarContent, tabcolEqualOrNot, tabcolSecondEarContent);
        JTableHeader tabheadRecycleBinManager = TableDesigner
                .createTableHeader(tabcolmdlRecycleBinManager);
        
        tableEARCompare = TableDesigner.createTable(cstbAddFields,
                tabcolmdlRecycleBinManager, tabheadRecycleBinManager, 0, 20,
                true, false, true, 0);
        tableEARCompare.setBackground(Color.WHITE);
        tableEARCompare.setForeground(Color.WHITE);
        
        CustomTableRenderer trpref = new CustomTableRenderer(this, 1);
        tableEARCompare.setDefaultRenderer(Object.class, trpref);

        tableEARCompare.setCursor(EARUtility.curHand);

        int intSize = vecFirstEARFilesContent.size();
        String strFirstEarContent = null;
        String strSecondEarContent = null;
        for(int i=0;i<intSize;i++)
        {
            strFirstEarContent = (String) vecFirstEARFilesContent.get(i);
            strSecondEarContent = (String) vecSecondEARFilesContent.get(i);
            
            strFirstEarContent = strFirstEarContent.replaceAll("/", ".");
            strSecondEarContent = strSecondEarContent.replaceAll("/", ".");
            
            tableEARCompare.setValueAt(strFirstEarContent,i,0);
            tableEARCompare.setValueAt(strSecondEarContent,i,2);
        }

        JScrollPane sclpanEARCompare = new JScrollPane(
                tableEARCompare, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        tableEARCompare.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent e)
            {
                int intClickCount = e.getClickCount();
                if(intClickCount == 2)
                {
                    int intSelectedRow = tableEARCompare.getSelectedRow();
                    if(intSelectedRow >= 0)
                    {
                        String strFirstEarContent = (String)tableEARCompare.getValueAt(intSelectedRow,0);
                        String strSecondEarContent = (String)tableEARCompare.getValueAt(intSelectedRow,2);
                        if((strFirstEarContent != null && strFirstEarContent.trim().length() > 0)
                                && (strSecondEarContent != null && strSecondEarContent.trim().length() > 0))
                        {
                            int intFirstEARContentDotLastIndex = strFirstEarContent.lastIndexOf(".");
                            int intSecondEARContentDotLastIndex = strSecondEarContent.lastIndexOf(".");
                            
                            String strFirstEARContentExtention = strFirstEarContent.substring(++intFirstEARContentDotLastIndex);
                            String strSecondEARContentExtention = strSecondEarContent.substring(++intSecondEARContentDotLastIndex);
                            String strFileDestinationPath1 = null;
                            String strFileDestinationPath2 = null;
                            
                            if((strFirstEARContentExtention.equals("jar") || 
                                    strFirstEARContentExtention.equals("ear") ||
                                    strFirstEARContentExtention.equals("war") ||
                                    strFirstEARContentExtention.equals("zip")) &&
                                    (strSecondEARContentExtention.equals("jar") || 
                                    strSecondEARContentExtention.equals("ear") ||
                                    strSecondEARContentExtention.equals("war") ||
                                    strSecondEARContentExtention.equals("zip")))
                            {
                                //Getting the exacte value from the ear file
                                try
                                {
                                    ZipInputStream in = new ZipInputStream(new FileInputStream(strGlobleFirstEARFileName));
//                                  Get the first entry
                                    ZipEntry entry = in.getNextEntry();
                                    
                                    String strFileDestinationPath = EARUtility.strGlobleTemperoryDirectoryLocation + new Date().getTime(); 

                                    while(entry != null)
                                    {
                                        String strFilePath = entry.getName();
                                        
                                        if(strFilePath.equals(strFirstEarContent))
                                        {
                                            strFileDestinationPath1 = strFileDestinationPath + "\\1";
                                            extractInnerFiles(strFirstEarContent, in, strFileDestinationPath1);
                                        }
                                        entry = in.getNextEntry();
                                    }
                                    //Extracting the second file 
                                    ZipInputStream in1 = new ZipInputStream(new FileInputStream(strGlobleSecondEARFileName));
//                                  Get the first entry
                                    ZipEntry entry1 = in1.getNextEntry();

                                    while(entry1 != null)
                                    {
                                        String strFilePath = entry1.getName();
                                        if(strFilePath.equals(strSecondEarContent))
                                        {
                                            strFileDestinationPath2 = strFileDestinationPath + "\\2";
                                            extractInnerFiles(strSecondEarContent, in1, strFileDestinationPath2);
                                        }
                                        entry1 = in1.getNextEntry();
                                    }
                                    in.close();
                                    in1.close();
                                    
                                    String strFinalPathForFirstContent = strFileDestinationPath1 + "\\" + strFirstEarContent;
                                    String strFinalPathForSecondContent = strFileDestinationPath2 + "\\" + strSecondEarContent;
                                    
                                    File f = new File(strFinalPathForFirstContent);
                                    
                                    new EARFilesComparingTool().compareFilesinEar(strGlobleFirstEARFileName, strGlobleSecondEARFileName, 
                                            strFinalPathForFirstContent, strFinalPathForSecondContent);
                                    
                                }
                                catch (IOException ioe)
                                {
                                    EARUtility.log4jEARUtility.fatal("Exception", ioe);
                                }
                            }
                        }
                    }
                }
            }
        });
        
        panlFirstEARFile.setLayout(new BorderLayout());
        panlFirstEARFile.add(sclpanEARCompare, BorderLayout.CENTER);
        
        btnHtmlReport = new JButton(imgicnInternerExplore);
        btnTextReport = new JButton(imgicnNotepad);
        btnHtmlReport.addActionListener(this);
        btnTextReport.addActionListener(this);
        
        panlSecondEARFile.setLayout(new FlowLayout(FlowLayout.LEFT));
        panlSecondEARFile.add(btnHtmlReport);
        panlSecondEARFile.add(btnTextReport);
        
        dlgEARFileCompareTool.add(panlFirstEARFile);
        dlgEARFileCompareTool.add(panlSecondEARFile, BorderLayout.NORTH);
        dlgEARFileCompareTool.setSize(EARUtility.intActualWidth, EARUtility.intActualHeight);
        dlgEARFileCompareTool.setVisible(true);
    }
        
    public void extractInnerFiles(String strEarFileContent, ZipInputStream in, String strFileDestinationPath)
    {
        File fileDestination = new File(strFileDestinationPath);
        boolean boolProjectAlreadyExists = fileDestination.exists();
        if(!boolProjectAlreadyExists)
        {
            boolean boolProjectCreated = fileDestination.mkdirs();
            try
            {
                String strOutputFileName = strFileDestinationPath + "\\" + strEarFileContent;
                OutputStream out = new FileOutputStream(strOutputFileName);
                
                byte[] byteFileBuffer = new byte[1024];
                int intFileContent;
                while ((intFileContent = in.read(byteFileBuffer)) > 0)
                {
                    out.write(byteFileBuffer, 0, intFileContent);
                }
                out.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
                EARUtility.log4jEARUtility.fatal("Exception", e);
            }
            
        }
    }
    
    public void showReportGenerateDialog()
    {
        dlgReportSelection = new JDialog(dlgEARFileCompareTool, "Report Generation", true);
        JPanel panlFileSelection = new JPanel();
        
        chkMissingFiles = new JCheckBox("Missing Files");
        chkChangedFiles = new JCheckBox("Changed Files");
        chkSimilarFiles = new JCheckBox("Similar Files");
        
        btnGenerateReport = new JButton("Generate");
        btnGenerateReport.addActionListener(this);
        
        panlFileSelection.setLayout(null);
        chkSimilarFiles.setBounds(20, 20, 100, 20);
        chkMissingFiles.setBounds(20, 45, 100, 20);
        chkChangedFiles.setBounds(20, 70, 110, 20);
        
        panlFileSelection.add(chkMissingFiles);
        panlFileSelection.add(chkChangedFiles);
        panlFileSelection.add(chkSimilarFiles);        
        
        TitledBorder titledBorder = BorderFactory.createTitledBorder("File Selection");
        panlFileSelection.setBorder(titledBorder);

        dlgReportSelection.setLayout(null);
        panlFileSelection.setBounds(10, 10, 140, 100);
        btnGenerateReport.setBounds(30, 117, 100, 20);
        
        dlgReportSelection.add(panlFileSelection);
        dlgReportSelection.add(btnGenerateReport);
        
        dlgReportSelection.setResizable(false);
        dlgReportSelection.setSize(170, 180);
        dlgReportSelection.setLocationRelativeTo(dlgEARFileCompareTool);
        dlgReportSelection.setVisible(true);
    }
    
	public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == btnHtmlReport)
        {
			strReportType = "Html";
            showReportGenerateDialog();
        }
        else if(e.getSource() == btnTextReport)
        {
			strReportType = "Text";
            showReportGenerateDialog();
        }
        else if(e.getSource() == btnGenerateReport)
        {
			if(strReportType.equals("Html"))
			{
                HtmlReportCreator objHtmlReportCreator = new HtmlReportCreator(this);
                objHtmlReportCreator.generateHTMLReport(EARUtility.strGlobleHTMLReportLocation);
			}
            else if(strReportType.equals("Text"))
            {
                TextReportCreator objTextReportCreator = new TextReportCreator(this);
                objTextReportCreator.generateTextReport(EARUtility.strGlobleTextReportLocation);
            }
        }
    }
}